﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


using Jungle.Code.Utils;
using Jungle.Code.Core;
using Jungle.Users;

namespace Jungle.Users.WebControls
{
    public class UserTree : WebControl, IPostBackEventHandler
    {
        private int OnPage = 30;
        private int CurrPage = 1;

        //------------------------------------------------------------------------------------------------------
        public void RaisePostBackEvent(string eventArgument)
        {
            int pos = eventArgument.IndexOf(',');
            int l = eventArgument.Length;
            string s1 = eventArgument.Substring(0, pos);
            string s2 = eventArgument.Substring(pos + 1, (l - (pos + 1)));

            if (s1 == "chP") // перелистывание страницы
            {
                CurrPage = Convert.ToInt32(s2);
            }
            if (s1 == "delUser") // удаление пользователя
            {
                UserManager delUser = new UserManager();
                delUser.UserId = Convert.ToInt32(s2);
                delUser.Delete();

            }
        }
        //--------------------------------------------------------------------
        protected override void RenderContents(HtmlTextWriter writer)
        {
            UserManager users = new UserManager();
            users.GetAll();

            writer.AddAttribute("style","padding: 10px;");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.Write("<strong>Пользователи:</strong>");

            for (int i = (OnPage * CurrPage) - OnPage; i <= (OnPage * CurrPage) - 1; i++)
            {
                if (i >= users.UserList.Count) break;
                UserManager user = users.UserList[i];

                writer.AddAttribute("style", "padding-left: 15px; padding-bottom: 5px;");
                writer.AddAttribute("onmouseover", "this.style.backgroundColor = \"#eee\";");
                writer.AddAttribute("onmouseout", "this.style.backgroundColor = \"\";");
                writer.RenderBeginTag(HtmlTextWriterTag.Div);
                writer.AddAttribute("style", "width: 100px;");
                writer.AddAttribute(HtmlTextWriterAttribute.Href, UrlHelper.GetAdminUrl() + "Users.aspx?cmd=show&id=" + user.UserId.ToString());
                writer.RenderBeginTag(HtmlTextWriterTag.A);
                writer.Write(user.Name + " " + user.Fname);
                writer.RenderEndTag();

                SessionKeeper sk = new SessionKeeper();
                sk.SessionId = Context.Session.SessionID;
                sk.GetData();
                UserManager um = new UserManager();
                um.UserId = sk.UserId;
                um.GetById();

                if (um.RoleId <= 2)
                {
                    // ------ Render edit buttom -----------
                    //writer.AddStyleAttribute(HtmlTextWriterStyle.Cursor, "pointer");
                    writer.AddAttribute("style", "margin-left: 10px;");
                    writer.AddAttribute("title", "Редактировать");
                    writer.AddAttribute(HtmlTextWriterAttribute.Href, UrlHelper.GetAdminUrl() + "Users.aspx?cmd=edit&id=" + user.UserId.ToString());
                    writer.RenderBeginTag(HtmlTextWriterTag.A);
                    PrintImage(writer, "/Admin/img/btn_edit.jpg");
                    writer.RenderEndTag();
                    // -------------------------------------

                    // ------ Redner delete buttom ---------
                    PrintBtnDel(writer, user.UserId);
                    // -------------------------------------
                }
                writer.RenderEndTag();
            }

            if (users.UserList.Count % OnPage == 0)
                RenderPageLister(writer, users.UserList.Count / OnPage);
            else
                RenderPageLister(writer, (users.UserList.Count / OnPage) + 1);


            writer.RenderEndTag();
            base.RenderContents(writer);
        }
        //-----------------------------------------------------------------------------------------------
        public HtmlTextWriter PrintBtnDel(HtmlTextWriter writer, int userId)
        {
            ClientScriptManager cs = Page.ClientScript;
            PostBackOptions opt = new PostBackOptions(this);
            opt.ActionUrl = Context.Request.RawUrl;
            opt.Argument = "delUser," + userId.ToString();

            writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "if(confirm(\"Вы действительно хотите удалить?\"))" + cs.GetPostBackEventReference(opt));
            writer.AddAttribute("title", "Удалить");
            writer.AddAttribute("style", "cursor: pointer;");
            PrintImage(writer, "/Admin/img/btn_delete.jpg");
            return writer;
        }
        //-----------------------------------------------------------------------------------------------
        public HtmlTextWriter PrintImage(HtmlTextWriter writer, string imgUrl)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Src, UrlHelper.GetSiteUrl() + imgUrl);
            writer.AddAttribute(HtmlTextWriterAttribute.Align, "absmiddle");
            writer.RenderBeginTag(HtmlTextWriterTag.Img);
            writer.RenderEndTag();
            return writer;
        }
        //-----------------------------------------------------------------------------------------------
        private HtmlTextWriter RenderPageLister(HtmlTextWriter writer, int count)
        {
            ClientScriptManager cs = Page.ClientScript;
            PostBackOptions opt = new PostBackOptions(this);
            opt.ActionUrl = Context.Request.RawUrl;

            writer.Write("<b>Страница: </b>");
            for (int i = 1; i <= count; i++)
            {
                if (i == CurrPage)
                    writer.Write("<b>" + i.ToString() + "</b>");
                else
                {
                    opt.Argument = "chP," + i.ToString();
                    writer.AddAttribute(HtmlTextWriterAttribute.Onclick, cs.GetPostBackEventReference(opt));
                    writer.AddAttribute("style", "cursor: pointer;");
                    writer.RenderBeginTag(HtmlTextWriterTag.Span);
                    writer.Write(i.ToString());
                    writer.RenderEndTag();
                }
                if (i < count) writer.Write(", ");
            }
            return writer;
        }
    }
}
